package com.hpin.mlxteacher.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.bean.SchoolResultBean;
import com.hpin.mlxteacher.utils.CommonUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

import java.util.List;

/**
 * Created by Aaron on 2015/7/21.
 */
public class ChooseAdapter extends BaseAdapter {

    private Context ctx;
    private List<SchoolResultBean.Data> datas;
    private DisplayImageOptions options;

    public ChooseAdapter(Context ctx, List<SchoolResultBean.Data> datas) {
        this.ctx = ctx;
        this.datas = datas;

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.icon_wumingtu)
                .showImageForEmptyUri(R.drawable.icon_wumingtu)
                .showImageOnFail(R.drawable.icon_wumingtu)
                .resetViewBeforeLoading(true)
                .cacheOnDisk(true).cacheInMemory(true)
                .cacheOnDisc(true).imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .considerExifParams(true)
                .displayer(new SimpleBitmapDisplayer()).build();
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setDatas(List<SchoolResultBean.Data> datas) {
        this.datas = datas;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=null;

        if (convertView == null) {

            convertView = View.inflate(ctx, R.layout.item_receiver, null);
            viewHolder = new ViewHolder();
            viewHolder.iv_receiver_pic = (ImageView) convertView.findViewById(R.id.iv_receiver_pic);
            viewHolder.iv_is_select = (ImageView) convertView.findViewById(R.id.iv_is_select);
            viewHolder.tv_receiver_name = (TextView) convertView.findViewById(R.id.tv_receiver_name);
            convertView.setTag(viewHolder);

        }else{
            viewHolder=(ViewHolder) convertView.getTag();
        }

//        final ImageView tempIv = viewHolder.iv_is_select;
//        viewHolder.iv_is_select.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                tempIv.setSelected(!tempIv.isSelected());
//            }
//        });
//     LogUtil.e("TAG", "peoples==>" + peoples);
//
//        People people = peoples.get(position);
//
//        LogUtil.e("TAG", "peopel==>" + people);
//
//        Boolean s = people.isSelect;
//        LogUtil.e("TAG", "viewHolder.iv_is_select==>" + viewHolder.iv_is_select);
//        viewHolder.iv_is_select.setSelected(peoples.get(position).isSelect == 1);
        viewHolder.iv_is_select.setSelected(datas.get(position).isSelect);
        viewHolder.tv_receiver_name.setText(datas.get(position).nickName);

        if (!CommonUtils.isNull(datas.get(position).imgUrl)) {
            ImageLoader.getInstance().displayImage(datas.get(position).imgUrl, viewHolder.iv_receiver_pic,options);
        }else {
            ImageLoader.getInstance().displayImage(datas.get(position).picUrl, viewHolder.iv_receiver_pic,options);
        }


        return convertView;
    }

    private class ViewHolder {
        private ImageView iv_receiver_pic;
        private ImageView iv_is_select;
        private TextView tv_receiver_name;

    }

}
